package demo.practice.leetcode_offer;

public class Offer59_2 {


    public static void main(String[] args) {
        Offer59_2 o2 = new Offer59_2();
                                         //  3，-1，1，
        o2.maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3);
    }

    public int[] maxSlidingWindow(int[] nums, int k) {

        if (nums == null || nums.length == 0 || k > nums.length)
            return new int[0];

        int partSum = 0;
        for (int i = 0; i < k; i++) {
            partSum += nums[i];
        }

        int max = partSum;

        //[3,3,5,5] k=3  则可以形成2个滑动窗口
        int[] maxs = new int[nums.length - k + 1];
        maxs[0] = partSum;

        for (int i = 1; i <= k; i++) {
            partSum = partSum + nums[i + k - 1] - nums[i];
            max = Math.max(max, partSum);
            maxs[i] = max;
        }

        return maxs;
    }


}
